
# 打开浏览器  访问  点击 输入  元素定位  iframe  窗口切换。。。
import time

import allure
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec

class WebKeys:
    # 浏览器运行  只要实例化WebKeys，就会走到init函数里面  打开浏览器
    def __init__(self,driver):
        self.driver=driver
        self.wait=WebDriverWait(self.driver,10, poll_frequency=0.01)

    # 访问  函数传参  return
    @allure.step("访问网址")
    def open(self,url):
        self.driver.get(url)

    # 找元素  等待
    # locator=(By.ID,"txtUName")
    # wait.until(ec.visibility_of_element_located(locator)).send_keys(phone_number)
    # driver.find_element(By.PARTIAL_LINK_TEXT, "文本").click()
    # 为什么返回 点击的时候 拿到元素

    def locator_with_wait(self,name,value):
        locator=(name,value)
        el=self.wait.until(ec.visibility_of_element_located(locator))
        self.locator_station(el)
        return el
    #等待元素变得不可见
    def mask_with_wait(self,name,value):
        locator=(name,value)
        self.wait.until(ec.invisibility_of_element_located(locator))


    def locators_with_wait(self,name,value):
        locator=(name,value)
        el=self.wait.until(ec.visibility_of_all_elements_located(locator))
        return el

    # 不确定我找的元素对不对 边框
    def locator_station(self,el):
        self.driver.execute_script(
            "arguments[0].setAttribute('style',arguments[1]);",
            el,
            'border:1px solid green;'
        )

    # 点击  上面找元素
    @allure.step("点击")
    def on_click(self,name,value):
        self.locator_with_wait(name,value).click()

    # 输入
    @allure.step("输入")
    def on_input(self,name,value,content):
        self.locator_with_wait(name,value).send_keys(content)

    # 等待 强制等待
    @allure.step("等待")
    def wait_time(self,content):
        time.sleep(content)

    # iframe 跳到iframe
    @allure.step("跳到iframe")
    def on_iframe(self,content):
        self.driver.switch_to.frame(content)

    # 退出iframe
    @allure.step("退出iframe")
    def iframe_home(self):
        self.driver.switch_to.default_content()

    # driver.find_element(xxx,xxx).text
    # self.locator_with_wait(el).text
    @allure.step("文本断言")
    def get_text(self,name,value):
        # el=(name,value)
        result=self.locator_with_wait(name, value)
        # result=self.wait.until(
        #     lambda x:x.find_element(*el).text
        # )
        return result.text

    @allure.step("文本模糊断言")
    def get_text_contains(self, name, value, expected_text):
        el = (name, value)
        result = self.wait.until(
            ec.text_to_be_present_in_element(el, expected_text)
        )
        return result


    @allure.step("地址断言")
    def get_text_true_wait(self,name,value,txt):
        el=(name,value)
        result=self.wait.until(ec.text_to_be_present_in_element(el,txt))
        return result


    @allure.step("切换窗口")
    def change_window(self,n):
        handles=self.driver.window_handles
        self.driver.switch_to.window(handles[n])

    @allure.step("跳转到浏览器alert弹窗")
    def change_alert(self, action):
        alert = self.driver.switch_to.alert
        if action == "accept":
            alert.accept()
        elif action == "dismiss":
            alert.dismiss()

